<?php

/**
 * This is the model class for table "admin_advertisement".
 *
 * The followings are the available columns in table 'admin_advertisement':
 * @property integer $id
 * @property integer $number
 * @property integer $typeId
 * @property integer $size
 * @property string $title
 * @property string $content
 * @property string $startTime
 * @property string $endTime
 * @property string $client
 * @property string $clientPhone
 * @property string $clientEmail
 * @property string $clientAddress
 * @property string $price
 * @property string $paymentType
 * @property string $paymentDate
 * @property string $memo
 *
 * The followings are the available model relations:
 * @property AdvertisementType $type
 */
class Advertisement extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Advertisement the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'admin_advertisement';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('number, typeId, size, title, content, startTime, endTime, client, clientPhone', 'required'),
			array('typeId', 'numerical', 'integerOnly'=>true),
			array('size','numerical','integerOnly'=>true,'min'=>1,'max'=>'60'),
			array('title, client, clientPhone, clientEmail, clientAddress, paymentType', 'length', 'max'=>255),
			array('clientEmail','email','on'=>'insert,update'),
			array('price', 'length', 'max'=>6),
			array('price','default','value'=>0),
			array('content','sizeLimitContent','on'=>'insert,update'),
			array('endTime','compareStartEndTime','on'=>'insert, update'),
			array('paymentDate','default','value'=>null),
			array('paymentDate, memo', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, number, typeId, size, title, content, startTime, endTime, client, clientPhone, clientEmail, clientAddress, price, paymentType, paymentDate, memo', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'type' => array(self::BELONGS_TO, 'AdvertisementType', 'typeId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'number' => 'Number',
			'typeId' => 'Type',
			'size' => 'Size',
			'title' => 'Title',
			'content' => 'Content',
			'startTime' => 'Start Time',
			'endTime' => 'End Time',
			'client' => 'Client',
			'clientPhone' => 'Client Phone',
			'clientEmail' => 'Client Email',
			'clientAddress' => 'Client Address',
			'price' => 'Price',
			'paymentType' => 'Payment Type',
			'paymentDate' => 'Payment Date',
			'memo' => 'Memo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('number',$this->number);
		$criteria->compare('typeId',$this->typeId);
		$criteria->compare('size',$this->size);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('startTime',$this->startTime,true);
		$criteria->compare('endTime',$this->endTime,true);
		$criteria->compare('client',$this->client,true);
		$criteria->compare('clientPhone',$this->clientPhone,true);
		$criteria->compare('clientEmail',$this->clientEmail,true);
		$criteria->compare('clientAddress',$this->clientAddress,true);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('paymentType',$this->paymentType,true);
		$criteria->compare('paymentDate',$this->paymentDate,true);
		$criteria->compare('memo',$this->memo,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public static function getAllPaymentTypes() {
		return array(
			'银行转账'=>'银行转账',
			'信用卡'=>'信用卡',
			'支票'=>'支票',
			'现金'=>'现金',
			'Online'=>'Online',
		);
	}
	
	public function sizeLimitContent($attribute,$params) {
		$size=$this->size;
		$num=strlen($this->content);
		$cnNum=0;
		for ($i = 0; $i < $num; $i++) {
			if (ord(substr($this->content, $i+1,1))>127) {
				$cnNum++;
				$i=$i+2;
			}
		}
		$enNum=$num-($cnNum*3);
		$number=($enNum/3)+$cnNum;
		
		$word_number = ceil($number);
		if ($word_number>$size*50) {
			$this->addError('content', 'The content word number cannot be bigger than '.$size*50);
		}
	}
	

	public function compareStartEndTime() {
		if ($this->getError('startTime')&&$this->getError('endTime')) {
			return ;//if there are other errors before this validate, ignore it.
		}
		if (strtotime($this->startTime)>=strtotime($this->endTime)) {
			$this->addError('startTime', 'The start time cannot be bigger or equal to the end time');
			$this->addError('endTime', 'The start time cannot be bigger or equal to the end time');
		};
	}
	
}